/**
 * 
 */
package com.android.dwt.fragment;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.dwt.R;
import com.android.dwt.data.GTDApp;
import com.android.dwt.ui.MainAct;

/**
 * @author xiang
 *
 */
public class MainFrag extends Fragment {
	
	private class MyPagerAdapter extends FragmentPagerAdapter {
        
        public MyPagerAdapter(FragmentManager fm) {
        	
			super(fm);
		}
        
        @Override
        public int getItemPosition(Object object) {
        	
        	Log.e(TAG, "getItemPosition is called!");
        	
        	return super.getItemPosition(object);
        }
        
		@Override
        public int getCount() {
			
//            return mListViews.size();
			
			return mFrags.size();
        }

		@Override
		public Fragment getItem(int arg0) {
			
//			return null;
			
//			return ArrayListFragment.newInstance(arg0);
			
			Log.e(TAG, "get item " + arg0);
			
			/*if (arg0 == TODAY) {
				
				mFrags.remove(TODAY);
				
				Cursor cursor = mGa.getDbta().getDayTask(mSdfDate.format(mToday.getTime()));
				if (cursor.getCount() > 0) {
					
					mFrags.add(TODAY, TodayAct.newInstance(true));
					
				} else {
					
					mFrags.add(TODAY, TodayAct.newInstance(false));
				}
				
				cursor.close();
			}*/
			
			return mFrags.get(arg0);
		}
    
	}		// end inner class

	
	private static final String TAG = "MainFrag";
	
	private GTDApp mGa;
	
	private MainAct mMain;
	private ViewPager mViewPager;
	private MyPagerAdapter mAdapter;
	private ArrayList<Fragment> mFrags;
	
	private Calendar mToday = Calendar.getInstance();
	private SimpleDateFormat mSdfDate = new SimpleDateFormat("yyyy-MM-dd");
	
	
	public static MainFrag newInstance() {
		
		MainFrag mf = new MainFrag();
		
		return mf;
	}
	
	@Override
	public void onAttach(Activity activity) {
		
		Log.e(TAG, "onAttach is called!");
		
		super.onAttach(activity);
		
		mMain = (MainAct) activity;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		Log.e(TAG, "onCreate is called!");
		
		super.onCreate(savedInstanceState);
		
		mGa = (GTDApp) mMain.getApplication();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		Log.e(TAG, "onCreateView is called!");
		
//		return super.onCreateView(inflater, container, savedInstanceState);
		
		View v = inflater.inflate(R.layout.main_frag, container, false);
		mViewPager = (ViewPager) v.findViewById(R.id.vp_content);
		
		return v;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		
		Log.e(TAG, "onActivityCreated is called!");
		
		super.onActivityCreated(savedInstanceState);
		
		mAdapter = new MyPagerAdapter(mMain.getSupportFragmentManager());
		mViewPager.setAdapter(mAdapter);
		
		initFrags();
        
        mViewPager.setCurrentItem(0);
	}
	
	private void initFrags() {
    	
    	mFrags = new ArrayList<Fragment>();
		
    	// list
		mFrags.add(ListFrag.newInstance());
		
		// today
		Cursor cursor = mGa.getDbta().getDayTask(mSdfDate.format(mToday.getTime()));
		if (cursor.getCount() > 0) {
			
			mFrags.add(TodayFrag.newInstance());
			
		} else {
			
			mFrags.add(TodayFrag.newInstance());
		}
		
		// context
		
		
		cursor.close();
    }
	
	@Override
	public void onStart() {
		
		Log.e(TAG, "onStart is called!");
		
		super.onStart();
	}
	
	@Override
	public void onResume() {
		
		Log.e(TAG, "onResume is called!");
		
		super.onResume();
	}
	
	@Override
	public void onPause() {
		
		Log.e(TAG, "onPause is called!");
		
		super.onPause();
	}
	
	@Override
	public void onStop() {
		
		Log.e(TAG, "onStop is called!");
		
		super.onStop();
	}
	
	@Override
	public void onDestroyView() {
		
		Log.e(TAG, "onDestroyView is called!");
		
		super.onDestroyView();
	}
	
	@Override
	public void onDestroy() {
		
		Log.e(TAG, "onDestroyView is called!");
		
		super.onDestroy();
	}

}
